package org.example.JVM.ASM.UseASM;


import org.example.JVM.ByteCode.Util.ByteCodeUtils;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.FieldVisitor;

import java.io.IOException;

import static org.objectweb.asm.Opcodes.ACC_PRIVATE;

/**
 * @description:
 * @author: farprt
 * @create: 2022-11-16 23:25
 **/
public class UseAsmModifyClass {

    public static void main(String[] args) throws IOException {
        String className = "org.example.JVM.ASM.UseASM.UseAsmModifyClass";
        ClassReader classReader = new ClassReader(className);
//        ClassWriter classWriter = new ClassWriter(0);
//        classReader.accept(classWriter, 0);
//        generateField(classWriter);
        MyClassWriter myClassWriter = new MyClassWriter(new ClassWriter(0));
        classReader.accept(myClassWriter, 0);
        byte[] newByteCode = myClassWriter.toByteArray();
        ByteCodeUtils.saveToFile(className, newByteCode);
    }


    private static void generateField(ClassWriter classWriter) {
        FieldVisitor fieldVisitor = classWriter.visitField(ACC_PRIVATE, "id", "Ljava/lang/String;", null, null);
        fieldVisitor.visitAnnotation("Llombok/Getter;", false);
    }
}